import { req_Go_Login, req_UserInfo, un_Login_req_fun } from '../../api/req_fun'
import { Message } from 'element-ui'
export default {
    namespaced: true,
    state: () => ({
        token: window.localStorage.getItem('token') || '',
        UserInfo: {}
    }),
    actions: {
        // 请求登录
        async get_Go_Login({commit, dispatch}, req_fromData) {
            const data = await req_Go_Login(req_fromData)
            if (data.code === 200) {
                // 将token存入本地
                window.localStorage.setItem('token', data.data.token)
                commit('SET_TOKEN', data.data.token)
                return 'ok'
            } else {
                return Promise.reject(new Error('fail'))
            }
        },
        // 获取用户详情信息
        async get_userInfo({ commit }) {
            const data = await req_UserInfo()
            if (data.code === 200) {
                commit('GET_USERINFO', data.data)
                return 'ok'
            } else {
                return Promise.reject(new Error('fail'))
            }
        },
        // 退出登录
        async un_Login() {
            const data = await un_Login_req_fun()
            if (data.code === 200) {
                return 'ok'
            } else {
                return Promise.reject(new Error('fail'))
            }
        },
        // 退出登录成功 清楚所有token 和 用户信息
        clear_token_userinfo({ commit }) {
            commit('CLEAR_TOKEN_USERINFO')
            // 并且清理本地存储的token
            window.localStorage.removeItem('token')
        }
    },
    mutations: {
        GET_USERINFO(state, data) {
            state.UserInfo = data
        },
        SET_TOKEN(state, data) {
            state.token = data;
        },
        CLEAR_TOKEN_USERINFO(state) {
            state.token = '';
            state.UserInfo = {};
        }
    },
    getters: {
    }
}